package com.eascs.ticket.configuration.controller;

import javax.inject.Inject;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.common.util.ObjectUtil;
import com.eascs.common.util.StringUtil;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.MessageRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.ticket.common.entity.EntityUtil;
import com.eascs.ticket.configuration.entity.TicketTypeStatus;
import com.eascs.ticket.configuration.entity.TicketTypeStatusUpdate;
import com.eascs.ticket.configuration.form.DeleteFlowForm;
import com.eascs.ticket.configuration.form.EditFlowForm;
import com.eascs.ticket.configuration.form.SaveFlowForm;
import com.eascs.ticket.configuration.service.TicketTypeStatusService;

@RestController
public class TicketTypeRestController extends BaseRestController {
	@Inject
	private TicketTypeStatusService ticketTypeStatusService;

	@RequestMapping(value = "/configuration/ticket-type/editFlow")
	public HtmlRestResult editFlow(EditFlowForm editFlowForm) {
		if (StringUtil.hasText(editFlowForm.getTicketTypeStatusId())) {
			TicketTypeStatus ticketTypeStatus = this.ticketTypeStatusService.getTicketTypeStatusById(editFlowForm.getTicketTypeStatusId());
			ViewContext.put("ticketTypeStatus", ticketTypeStatus);
		} else {
			TicketTypeStatus ticketTypeStatus = new TicketTypeStatus();
			ticketTypeStatus.setTicketTypeCode(editFlowForm.getTicketTypeCode());
			ViewContext.put("ticketTypeStatus", ticketTypeStatus);
		}
		return HtmlRestResult.popHtmlFragment("configuration/ticketType/editFlow");
	}

	@RequestMapping(value = "/configuration/ticket-type/saveFlow")
	public BaseRestResult<String> saveFlow(SaveFlowForm saveFlowForm) {
		if (StringUtil.hasText(saveFlowForm.getId())) {
			TicketTypeStatusUpdate ticketTypeStatusUpdate = new TicketTypeStatusUpdate();
			ObjectUtil.copySimpleProperties(saveFlowForm, ticketTypeStatusUpdate);
			EntityUtil.initUpdateEntity(ticketTypeStatusUpdate);
			this.ticketTypeStatusService.updateTicketTypeStatus(ticketTypeStatusUpdate);
		} else {
			TicketTypeStatus ticketTypeStatus = new TicketTypeStatus();
			ObjectUtil.copySimpleProperties(saveFlowForm, ticketTypeStatus);
			EntityUtil.initInsertEntity(ticketTypeStatus);
			this.ticketTypeStatusService.insertTicketTypeStatus(ticketTypeStatus);
		}
		return MessageRestResult.refresh();
	}

	@RequestMapping(value = "/configuration/ticket-type/deleteFlow")
	public BaseRestResult<String> deleteFlow(DeleteFlowForm deleteFlowForm) {
		this.ticketTypeStatusService.deleteTicketTypeStatus(deleteFlowForm.getTicketTypeStatusId());
		return MessageRestResult.refresh();
	}
}
